using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Security.Permissions;

namespace OracleInternal.I18N
{
	[Serializable]
	internal class TLBConvBoot
	{
		private static TLBConvBoot upCache;

		private const string FILENAME = "lx0boot.glb";

		public const int FLAG_FIXEDWIDTH = 256;

		public const int FLAG_ASCII = 16;

		public const int FLAG_EBCDIC = 1;

		internal Dictionary<string, string> charSetIdMap;

		internal Dictionary<string, string> idtoCharSetMap;

		internal Dictionary<string, string> idtoCharSetMaxLen;

		internal IList<string> lstCharSetIsAscii;

		internal IList<string> lstCharSetIsEbcdic;

		internal IList<string> lstCharSetIsFixed;

		internal IList<string> lstCharSetIsStorage;

		internal string[] availableCharSet;

		public static TLBConvBoot GetInstance()
		{
			try
			{
				if (upCache == null)
				{
					upCache = (TLBConvBoot)ReadObj("lx0boot.glb");
				}
			}
			catch
			{
				return null;
			}
			return upCache;
		}

		public string GetCharSetFileName(string charSetName)
		{
			return FormatFileName("lx2", charSetIdMap[charSetName.ToUpper(new CultureInfo("en-US"))]);
		}

		public string GetCharSetId(string charSetName)
		{
			if (charSetIdMap.ContainsKey(charSetName.ToUpper(new CultureInfo("en-US"))))
			{
				return charSetIdMap[charSetName.ToUpper(new CultureInfo("en-US"))];
			}
			return string.Empty;
		}

		public string GetCharSetName(string id)
		{
			if (idtoCharSetMap.ContainsKey(id))
			{
				return idtoCharSetMap[id];
			}
			return string.Empty;
		}

		public string GetCharsetMaxCharLen(string id)
		{
			string result = "-1";
			if (!idtoCharSetMaxLen.ContainsKey(id) && id != null && id.Equals(GetCharSetId("AL16UTF16LE")))
			{
				return idtoCharSetMaxLen[GetCharSetId("AL16UTF16")];
			}
			if (!idtoCharSetMaxLen.ContainsKey(id))
			{
				return result;
			}
			return idtoCharSetMaxLen[id];
		}

		public IList<string> GetCharSetIsAscii()
		{
			return lstCharSetIsAscii;
		}

		public IList<string> GetCharSetIsEbcdic()
		{
			return lstCharSetIsEbcdic;
		}

		public IList<string> GetCharSetIsFixed()
		{
			return lstCharSetIsFixed;
		}

		public IList<string> GetCharSetIsStorage()
		{
			return lstCharSetIsStorage;
		}

		public string[] GetAvailableCharacterSets()
		{
			return availableCharSet;
		}

		protected static string FormatFileName(string prefix, string id)
		{
			string text = "0000";
			int num = Convert.ToInt32(id);
			string text2 = $"{num:X}";
			string str = text.Substring(0, text.Length - text2.Length) + text2;
			return prefix + str;
		}

		[SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
		public static object ReadObj(string entryName)
		{
			Stream? manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("OracleInternal.I18N." + entryName);
			object obj = null;
			using GZipStream stream = new GZipStream(manifestResourceStream, CompressionMode.Decompress, leaveOpen: true);
			return TLBConvSerializer.Deserialize(stream, entryName);
		}

		public void SetAvailableCharSets(string[] array)
		{
			availableCharSet = array;
		}

		public void setCharSet(Dictionary<string, string> h)
		{
			charSetIdMap = h;
		}

		public void setIdtoCharSet(Dictionary<string, string> h)
		{
			idtoCharSetMap = h;
		}

		public void setCharSetMaxLen(Dictionary<string, string> h)
		{
			idtoCharSetMaxLen = h;
		}

		public void setCharSetFlags(IList<string> isAscii, IList<string> isEbcdic, IList<string> isFixed, IList<string> isStorage)
		{
			lstCharSetIsAscii = isAscii;
			lstCharSetIsEbcdic = isEbcdic;
			lstCharSetIsFixed = isFixed;
			lstCharSetIsStorage = isStorage;
		}

		public Dictionary<string, string> getCharSetName2IdMap()
		{
			return charSetIdMap;
		}
	}
}
